﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-25 16:18:41
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.Components
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.IO;

    /// <summary>
    /// 内容模型授权。
    /// </summary>
    public class Licenses
    {
        private Licenses(XmlDocument document)
        {
            this.document = document;
            Initialized();
        }

        private void Initialized()
        {
            if(document == null)
                return;
            var node = document.SelectSingleNode("license");
            LicenseKey = node["key"].GetInnerText();
            if(LicenseKey == null)
                return;
            var license = node["value"].GetInnerText();
            if(license == null)
                return;
            license = Cores.Decrypt(Cryptography.EncryptionAlgorithm.Rijndael, Cores.Decrypt(Cryptography.EncryptionAlgorithm.Des, license, LicenseKey), LicenseKey);

            if(license == null)
                return;
            var doc = new XmlDocument();
            doc.LoadXml(license);
            foreach(XmlNode dNode in doc.SelectSingleNode("licenses")) {
                if(dNode.NodeType == XmlNodeType.Comment)
                    continue;
                values.Add(dNode.Name, dNode.GetInnerText());
            }
        }

        private XmlDocument document;
        private static readonly string path = Cores.MapPath("mozcms.lic");
        private static Licenses licenses;
        private static readonly object locker = new object();
        private Dictionary<string, string> values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        /// <summary>
        /// 授权ID。
        /// </summary>
        public string LicenseKey { get; private set; }
        /// <summary>
        /// 获取授权值。
        /// </summary>
        /// <param name="key">授权键。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回当前授权实例对象。</returns>
        public string Get(string key, string defaultValue = null) {
            string value;
            if(values.TryGetValue(key, out value))
                return value;
            return defaultValue;
        }
        /// <summary>
        /// 获取授权值。
        /// </summary>
        /// <typeparam name="T">返回类型。</typeparam>
        /// <param name="key">授权键。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>返回当前授权实例对象。</returns>
        public T Get<T>(string key, T defaultValue = default(T)) {
            return Get(key).To<T>(defaultValue);
        }
        /// <summary>
        /// 获取当前授权实例对象。
        /// </summary>
        public static Licenses Current {
            get {
                lock(locker)
                {
                    if(licenses == null)
                    {
                        if(!File.Exists(path))
                            licenses = new Licenses(null);
                        else
                        {
                            var document = new XmlDocument();
                            document.Load(path);
                            licenses = new Licenses(document);
                        }
                    }
                    return licenses;
                }
            }
        }
    }
}
